package com.srd.phone.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shengrongdai.srd.R;
import com.srd.phone.bean.TenderDetailInfo;
import com.srd.phone.bean.TenderInfo;
import com.srd.phone.mvp.presenters.BasePresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.LogUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 标的详情
 */
public class TenderDetailActivity extends BaseActivity implements View.OnClickListener {

    private CheckBox cb_tender_record;
    private TextView tv_tender_record_content; //投资记录详情
    private LinearLayout ll_tender_people_info; //借款人个人信息
    private LinearLayout ll_identity_approve; //身份认证
    private LinearLayout ll_car_approve; //车产证明
    private LinearLayout ll_house_approve; //房产证明

    private String urlBmarkListDetails = Constants.BASEURL + "bmarkApp/bmarkIntroduce";
    private TenderInfo info;
    private TenderDetailInfo detailInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        info = (TenderInfo) getIntent().getSerializableExtra("info");
        setContentView(R.layout.activity_tender_detail);
        initView();
        initData();
    }

    @Override
    public void initView() {
        tv_common_title.setText("标的项目介绍");
        cb_tender_record = (CheckBox) findViewById(R.id.cb_tender_record);
        tv_tender_record_content = (TextView) findViewById(R.id.tv_tender_record_content);
        ll_tender_people_info = (LinearLayout) findViewById(R.id.ll_tender_people_info);
        ll_identity_approve = (LinearLayout) findViewById(R.id.ll_identity_approve);
        ll_identity_approve.setOnClickListener(this);
        ll_car_approve = (LinearLayout) findViewById(R.id.ll_car_approve);
        ll_car_approve.setOnClickListener(this);
        ll_house_approve = (LinearLayout) findViewById(R.id.ll_house_approve);
        ll_house_approve.setOnClickListener(this);

        cb_tender_record.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tv_tender_record_content.setVisibility(View.VISIBLE);
                } else {
                    tv_tender_record_content.setVisibility(View.GONE);
                }
            }
        });
        cb_tender_record.setChecked(true);

    }

    @Override
    public void initData() {
        presenter = new BasePresenter(requestQueue, this);
        setUrlArrs(urlBmarkListDetails);
        showWaitingDialog();
        Map<String, String> map = new HashMap<>();
        map.put("id", info.id + "");
        presenter.requestPostData(urlBmarkListDetails, map);
    }

    private void setData() {
        if (detailInfo == null)
            return;
        tv_tender_record_content.setText(detailInfo.details);
        ll_tender_people_info.removeAllViews();
        addPeopleInfo("性别", detailInfo.sex + "");
        addPeopleInfo("年龄", detailInfo.age + "岁");
        addPeopleInfo("学历", detailInfo.education);
        addPeopleInfo("子女状况", detailInfo.families);
        addPeopleInfo("户籍城市", detailInfo.domicile);
        addPeopleInfo("婚姻状况", detailInfo.marriage);
    }

    private void addPeopleInfo(String name, String value) {
        View view = getLayoutInflater().inflate(R.layout.layout_child_item, null);
        TextView tv_name = (TextView) view.findViewById(R.id.tv_item_name);
        TextView tv_value = (TextView) view.findViewById(R.id.tv_item_value);
        tv_name.setText(name);
        tv_value.setText(value);
        ll_tender_people_info.addView(view);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        LogUtils.i(TenderDetailActivity.class, data);
        cancelWaitingDialog();
        detailInfo = TenderDetailInfo.parse(data);
        setData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_identity_approve:
                gotoActivity(TenderPeopleIdentityCardFrontActivity.class);
                break;
            case R.id.ll_car_approve:
                gotoActivity(TenderCarApproveFrontActivity.class);
                break;
            case R.id.ll_house_approve:
                gotoActivity(TenderHouseApproveActivity.class);
                break;
        }
    }
}
